<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_fahuo/model')->方法名();
*/
namespace jy_fahuo;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//https://global.link.cainiao.com/#/homepage/api/logistics/electronic_sheet/TMS_WAYBILL_APPLY_NEW?_k=9ay4kw
	//面单申请-获取面单号
	public function waybill_apply($order_id){
		
	}
	public function orderprint($order_id){
		
	}
	public function get_templateUrl($cpCode='',$type=''){
		$prams=array('cpcode'=>$cpCode,'uniacid'=>$_SESSION['uniacid']);
		if($type){
			$prams['type']=$type;
		}
		$templateUrl=pdo_getcolumn('jy_fahuo_tpl',$prams,'url');
		if(!$templateUrl){
			exi('不存在该物流公司的面单模板！','error');
		}
		return $templateUrl;
	}
	public function upprintmessage($order=array(),$data=array()){
		$basic=plugin('jy_fahuo')->plugin_setting('basic');
		$documents=array();
		$contents=array();
		if(!is_array($order['address'])){
			$order['address']=json_decode($order['address'],true);
		}
		if(!is_array($data['printdata'])){
			$data['printdata']=json_decode($data['printdata'],true);
		}
		$address=array(
			'province'=>$order['address']['pcd']['province'],
			'city'=>$order['address']['pcd']['city'],
			'district'=>$order['address']['pcd']['district']?:'',
			'town'=>$order['address']['pcd']['street']?:'',
			'detail'=>$order['address']['address']?:'',
		);
		$recipient=array(
			'address'=>$address,
			'phone'=>$order['phone']?:'',
			'mobile'=>$order['address']['mobile']?:$order['mobile'],
			'name'=>$order['address']['name']?:$order['name'],
		);
		$address=array(
			'province'=>$basic['pcd']['province'],
			'city'=>$basic['pcd']['city'],
			'district'=>$basic['pcd']['district'],
			'town'=>$basic['pcd']['street'],
			'detail'=>$basic['address'],
		);
		$sender=array(
			'address'=>$address,
			'phone'=>$basic['phone']?:'',
			'mobile'=>$basic['mobile'],
			'name'=>$basic['name'],
		);
		$routingInfo=array(
			'consolidation'=>array('name'=>'','code'=>''),
			'origin'=>array('name'=>'','code'=>''),
			'sortation'=>array('name'=>'','code'=>''),
			'routeCode'=>'',
		);
		$routingInfo=$data['printdata']['routingInfo'];
		$shippingOption=$data['printdata']['shippingOption'];
		$msgdata=array(
			'recipient'=>$recipient,
			'routingInfo'=>$routingInfo,
			'sender'=>$sender,
			'shippingOption'=>$shippingOption,
			'waybillCode'=>$data['cpcode'],
		);
		$contents1=array(
			'data'=>$msgdata,
			'templateURL'=>$data['templateurl'],
		);
		$contents[]=$data['printdata'];
		$documents1=array(
			'documentID'=>$data['cpcode'],
			'contents'=>$contents,
		);
		$documents[]=$documents1;
		$task=array(
			'taskID'=>$data['id'],
			'preview'=>$data['preview'],
			'printer'=>'',
			'notifyMode'=>'allInOne',
			'previewType'=>'image',
			'documents'=>$documents,
		);
		$printmessage=array(
			'cmd'=>'print',
			'requestID'=>$order['id'],
			'version'=>'1.0',
			'task'=>$task,
		);
		return $printmessage;
	}
	public function uplog($order=array(),$data=array()){
		$data=array(
			'cpcode'=>$data['cpcode'],
			'express_id'=>$data['express'],
			'expresssn'=>$data['expresssn'],
			'templateurl'=>$data['templateurl'],
			'starttime'=>$data['starttime'],
			'endtime'=>$data['endtime'],
			'orderid'=>$order['id'],
			'ordersn'=>$order['ordersn'],
			'merchid'=>$order['merchid'],
			'printdata'=>$data['printdata'],
		);
		$data['createtime']=TIMESTAMP;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_fahuo_log',$data);
		$logid=pdo_insertid();
		return $logid;		
	}
}
?>